var gulp=require('gulp'),
	browserSync = require('browser-sync'),
	reload = browserSync.reload;

var proxyMiddleware = require('http-proxy-middleware');
var nodemon = require('gulp-nodemon');
var concat=require('gulp-concat');
var uglify=require('gulp-uglify');
var rename=require('gulp-rename');
var cache = require('gulp-cache');
var imagemin = require('gulp-imagemin'); //压缩图片
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var useref = require('gulp-useref');
var htmlmin = require('gulp-htmlmin');
var gulpif = require('gulp-if');
var del = require('del');

const pathConfig={
    "css": {
        "src": ["app/static/styles/*.css", "app/static/styles/**/*.css"],
        "dist": "dist/app/static/styles/"
    },
    "images": {
        "src": "app/static/image/**/*",
        "dist": "dist/app/static/image/"
    },
    "js": {
        "src": ["app/static/scripts/*.js","app/static/scripts/**/*.js"],
        "dist": "dist/app/static/scripts/"
    },
    'html':{
    	"src": "index.html",
    	"dist": "dist/"
    },
    "tpl":{
    	"src":['app/static/page/*.html','app/static/page/**/*.html'],
    	"dist": "dist/app/static/page/"
    }
}


//图片
gulp.task('images', function () {
	return gulp.src(pathConfig.images.src)
	    .pipe(cache(imagemin({
	        progressive: true,
	        interlaced: true
	    })))
	    .pipe(gulp.dest(pathConfig.images.dist));
});



//压缩css
gulp.task('Cssmain',function(){	
    return gulp.src(pathConfig.css.src)         
     	.pipe(minifyCss())
        .pipe(gulp.dest(pathConfig.css.dist))
});

//压缩js
gulp.task('Jsmain',function(){	
    return gulp.src(pathConfig.js.src)         
     	.pipe(uglify())
        .pipe(gulp.dest(pathConfig.js.dist))
});

//提取html页面的js,css文件进行处理
gulp.task('html', function () {
    return gulp.src(pathConfig.html.src)
        .pipe(useref())
        .pipe(gulpif('*.js', uglify()))
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(gulp.dest(pathConfig.html.dist));
});

//模板视图 views
gulp.task('tpl', function () {
  return gulp.src(pathConfig.tpl.src)
  		.pipe(htmlmin({
  			removeComments: true,//清除HTML注释
        	collapseWhitespace: true,//压缩HTML
        	collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
	        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
	        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
	        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
  		}))
  		.pipe(gulp.dest(pathConfig.tpl.dist))
});


gulp.task('clean', function () {
  	return del.bind(null, ['.tmp', 'dist/*']);
});

//实时监听入口文件
gulp.task('nodemon',() => {
  nodemon({ script: 'bin/www',
    ignore: ['README.md', 'node_modules/**', '.DS_Store']
  });
});



//开发运行
gulp.task('server',["nodemon"],function() {
    var files = [
        './app/*',
        './app/page/**/*.html',
         './app/static/scripts/*.js',
        './app/static/scripts/**/*.js',
        './app/static/styles/*.css',
    ];
//  var middleware = proxyMiddleware(['/api','/admin_login','/admin_regist','/admin/loadData'], {
//  	target: 'http://localhost:7893/', 
//  	changeOrigin: true
//  });
	browserSync.init(files, {
        proxy: 'http://localhost:20170',		//代理地址，和bin/www里的端口号要一致
        browser: 'chrome',
        notify: false,
        directoryListing: false,
        port: 20171
    });
//  browserSync.init({	
//      browser: 'chrome',
//      notify: true,
//      port: 9191,
//      server: {
//          baseDir: "./",
//          index:'index.html'
//      },
//     
//  });

    gulp.watch(files).on("change", reload); 
});


//综合打包
//gulp.task('build',['clean','ue','fonts','images','html','tpl'],function(){
//	console.log('build success');
//})


gulp.task('default',function () {
  	gulp.start('server');
});
